package com.qk.controller;

import com.qk.common.Result;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
   /* @PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile file) throws Exception {
        log.info("用户名:{},年龄:{},上传文件:{}",username,age,file.getOriginalFilename());
        //获取上传的文件名称---UUID设置文件名
        String fileName = file.getOriginalFilename();
        String fileNewName= UUID.randomUUID().toString();
        //截取.后的文件类型名称
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        String newFileName=fileNewName+fileType;

        //将文件保存到指定的磁盘
        file.transferTo(new File("D:\\images\\"+newFileName));
        return Result.success();
    }*/
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
        log.info("上传文件:{}",image.getOriginalFilename());
        //获取上传的文件名称---UUID设置文件名
        String fileName = image.getOriginalFilename();
        String fileNewName= UUID.randomUUID().toString();
        //截取.后的文件类型名称
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        String newFileName=fileNewName+fileType;
        String url = aliyunOSSOperator.upload(image.getBytes(), newFileName);
        return Result.success(url);
    }
}
